package com.hrbxlh.action;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hrbxlh.actionImpl.WSMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2018/5/18.
 */
@Controller
@RequestMapping("/message")
public class MessageController{
    //websocket服务层调用类
    @Autowired
    private WSMessageService wsMessageService;

    //请求入口
    @RequestMapping(value="/TestWS",method= RequestMethod.GET)
    @ResponseBody
    public String TestWS(@RequestParam(value="userId",required=true) Long userId,
                         @RequestParam(value="message",required=true) String message){
        if(wsMessageService.sendToAllTerminal(userId, message)){
            return "发送成功";
        }else{
            return "发送失败";
        }
    }
    //请求入口
    @RequestMapping(value="/TestLogin",method= RequestMethod.GET)
    @ResponseBody
    public ModelAndView TestLogin(HttpServletResponse response, String userId) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        Map<String,Object> resultMap = new HashMap<String,Object>();
        Map<String,Object> returnMap = new HashMap<String,Object>();
        resultMap.put("userId",userId);
       String reJsonStr = mapper.writeValueAsString(resultMap);
        returnMap.put("result", reJsonStr);
        System.out.println("返回的  :" + reJsonStr);
        String pageName = "login2";
        return new ModelAndView(pageName, returnMap);

    }
}
